package sprinklerbee.views;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import sprinklerbee.factory.ServiceFactory;
import sprinklerbee.model.Scheduler;
import sprinklerbee.model.Sprinkler;
import sprinklerbee.utils.SprinklerTabelModel;

import java.awt.GridLayout;
import javax.swing.ListSelectionModel;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class SchedulePanel extends JPanel{
	
	public static JTable table;

	/**
	 * Create the panel.
	 */
	public SchedulePanel() {
		setLayout(new GridLayout(1, 1, 0, 0));

		JScrollPane scrollPane = new JScrollPane();

		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					int row = table.getSelectedRow();
					Scheduler scheduler=new Scheduler();
					scheduler.setId(Integer.parseInt(String.valueOf(table.getValueAt(row, 0))));
					scheduler.setSprinkler(new Sprinkler(String.valueOf(table.getValueAt(row,1))));
					scheduler.setStarttime(String.valueOf(table.getValueAt(row, 2)));
					scheduler.setEndtime(String.valueOf(table.getValueAt(row, 3)));
					scheduler.setMon("Yes".equals(String.valueOf(table.getValueAt(row, 4)))?true:false);
					scheduler.setTue("Yes".equals(String.valueOf(table.getValueAt(row, 5)))?true:false);
					scheduler.setWed("Yes".equals(String.valueOf(table.getValueAt(row, 6)))?true:false);
					scheduler.setThur("Yes".equals(String.valueOf(table.getValueAt(row, 7)))?true:false);
					scheduler.setFri("Yes".equals(String.valueOf(table.getValueAt(row, 8)))?true:false);
					scheduler.setSat("Yes".equals(String.valueOf(table.getValueAt(row, 9)))?true:false);
					scheduler.setSun("Yes".equals(String.valueOf(table.getValueAt(row, 10)))?true:false);
					ScheduleSettingPanel.selectScheduler(scheduler);
				}
			}
		});
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane.setViewportView(table);
		add(scrollPane);
		setScheduleInfoData(new HashMap<String, String>());
	}

	public static void setScheduleInfoData(Map<String, String> criteria) {
		DefaultTableModel model = new SprinklerTabelModel();
		Vector v = new Vector();
		v.add("ID");
		v.add("Sprinkler");
		v.add("Start");
		v.add("End");
		v.add("Mon");
		v.add("Tue");
		v.add("Wed");
		v.add("Thur");
		v.add("Fri");
		v.add("Sat");
		v.add("Sun");
		Vector<Vector> data = null;
		if (null!=table) {
			if (null != criteria.get("groupName")
					&& !"".equals(criteria.get("groupName"))) {
				data = ServiceFactory.getSchedulerServiceInstance()
						.getScheduleByGroup(criteria.get("groupName"));
				model.setDataVector(data, v);
				table.setModel(model);
			} else if (null != criteria.get("sprinklerName")
					&& !"".equals(criteria.get("sprinklerName"))) {
				data = ServiceFactory.getSchedulerServiceInstance()
						.getScheduleBySprinkler(criteria.get("sprinklerName"));
				model.setDataVector(data, v);
				table.setModel(model);
			} else {
				data = ServiceFactory.getSchedulerServiceInstance()
						.getAllSchedules();
				model.setDataVector(data, v);
				table.setModel(model);
			}
		}
	}
	
	public static int getDeleteItemId() {
		int i = JOptionPane.showConfirmDialog(null,
				"Are you sure to delete the item?", "Tip", 0);
		if (i == 0) {
			int row = table.getSelectedRow();
			int id = Integer.parseInt(String.valueOf(table.getValueAt(row, 0)).trim());
			return id;
		}else{
			return 0;
		}
	}

}
